"""
@Project:p7_2109
@File:channel.py
@Author:马可
@Date:下午 02:33
"""

from flask import Blueprint
from flask_restful import Api,Resource,marshal,reqparse

from models.user import Channel,News
from common.model_fields.user import channel_fields,news_fields

channel_bp = Blueprint('channel',__name__,)

api = Api(channel_bp)


class ChannelsResource(Resource):
    def get(self):
        """获取所有的频道信息"""
        channels = Channel.query.all()
        return marshal(channels, channel_fields)

class NewsByChannel(Resource):
    def get(self):
        # 根据文章id，获取文章信息
        parser = reqparse.RequestParser()
        parser.add_argument('category')
        args = parser.parse_args()
        cate_id = args.get('category')
        news = News.query.filter_by(channel_id=cate_id).all()
        data = marshal(news, news_fields)
        # TODO 把用户信息返回
        return 'hhaha'



api.add_resource(ChannelsResource,'/channels')
api.add_resource(NewsByChannel,'/news/channel/')